/*
 * Copyright (C) 2015 cepardov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package models.view;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import system.utils.Modnames;
import system.utils.Service;

/**
 * @author cepardov <cepardov@gmail.com>
 */
public class Escritorio extends javax.swing.JFrame {
    boolean iniciado=false;
    Modnames modnames=new Modnames();
    Ventas ventas=new Ventas();
    Proveedor proveedores=new Proveedor();
    /**
     * Creates new form Desktop
     * @param NombreUsuario Nombre del usuario que inicia sesión
     */
    public Escritorio(String NombreUsuario) {
        initComponents();
        this.setTitle(modnames.getNombreAplicacion()+" ["+modnames.getInicioSesion()+NombreUsuario+"] - "+modnames.getModuloBase());
        this.setNombreUsuario(NombreUsuario);
        this.precargaModulos();
        this.inputCommand();//Siempre al final
    }
    
    public Escritorio(){initComponents();}
    
    public final void setNombreUsuario(String nombreUsuario){
        mnUser.setText(nombreUsuario);
    }
    
    public final void inputCommand(){
        this.txtComand.requestFocusInWindow();
    }
    
    private void precargaModulos(){
        this.ModuloVentas();
        this.ModuloInventario();
    }
    //
    private void ModuloVentas(){
        this.deskpane.add(ventas);
        ventas.setTitle("Ventas");
        try {
            ventas.setMaximum(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(Escritorio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void ModuloInventario(){
        this.deskpane.add(proveedores);
        proveedores.setTitle("Gestor de Proveedores");
    }
    
    @Override
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().
                getImage(ClassLoader.getSystemResource("iconos/market-64px.png"));
        return retValue;
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        deskpane = new javax.swing.JDesktopPane();
        lblestado = new javax.swing.JLabel();
        txtComand = new javax.swing.JTextField();
        Menu = new javax.swing.JMenuBar();
        mnUser = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnSistema = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        btnVenta = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setExtendedState(6);
        setIconImage(getIconImage());
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        deskpane.setBackground(new java.awt.Color(45, 45, 45));
        deskpane.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentRemoved(java.awt.event.ContainerEvent evt) {
                deskpaneComponentRemoved(evt);
            }
        });
        deskpane.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                deskpaneFocusGained(evt);
            }
        });
        deskpane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deskpaneMouseClicked(evt);
            }
        });
        deskpane.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                deskpaneComponentResized(evt);
            }
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                deskpaneComponentMoved(evt);
            }
            public void componentShown(java.awt.event.ComponentEvent evt) {
                deskpaneComponentShown(evt);
            }
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                deskpaneComponentHidden(evt);
            }
        });
        deskpane.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                deskpanePropertyChange(evt);
            }
        });

        lblestado.setText("Barra de estado");
        lblestado.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        txtComand.setFont(new java.awt.Font("Ubuntu", 0, 5)); // NOI18N
        txtComand.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComandKeyPressed(evt);
            }
        });

        mnUser.setText("Usuario");

        jMenuItem8.setText("Cambiar Contraseña");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        mnUser.add(jMenuItem8);

        jMenuItem2.setText("Cerrar Sesión");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        mnUser.add(jMenuItem2);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Cerrar");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        mnUser.add(jMenuItem1);

        Menu.add(mnUser);

        mnSistema.setText("Sistema");

        jMenuItem9.setText("Gestión de Usuarios");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        mnSistema.add(jMenuItem9);

        Menu.add(mnSistema);

        jMenu1.setText("Venta [F1]");

        btnVenta.setText("Venta");
        btnVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVentaActionPerformed(evt);
            }
        });
        jMenu1.add(btnVenta);

        Menu.add(jMenu1);

        jMenu2.setText("Compras [F2]");

        jMenuItem5.setText("Compras");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem4.setText("Proveedores");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        Menu.add(jMenu2);

        jMenu3.setText("Credito [F3]");

        jMenuItem6.setText("Creditos");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem6);

        Menu.add(jMenu3);

        jMenu4.setText("Finanzas [F4]");

        jMenuItem7.setText("Finanzas");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem7);

        Menu.add(jMenu4);

        jMenu6.setText("Ayuda");

        jMenuItem3.setText("Acerca de..");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem3);

        Menu.add(jMenu6);

        setJMenuBar(Menu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(deskpane, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblestado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtComand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(deskpane, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblestado)
                    .addComponent(txtComand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // Menu Cerrar base
        new Service().cerrarApp();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void btnVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVentaActionPerformed
        // TODO add your handling code here:
        System.out.println("Ventas\n");
        ventas.setVisible(true);
        ventas.txtInputCommand.requestFocusInWindow();
        this.lblestado.setText("Ventas: Realize ventas utilizando su tarjeta de vendedor para iniciar una venta.");
    }//GEN-LAST:event_btnVentaActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        // TODO add your handling code here:
        System.out.println("Compras\n");
        Compras compras=new Compras();
        this.deskpane.add(compras);
        compras.setTitle("Ingreso de Compras Productos");
        compras.setVisible(true);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
        System.out.println("Credito\n");
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        // TODO add your handling code here:
        System.out.println("Finanzas\n");
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        // TODO add your handling code here:
        CambiarClave clave=new CambiarClave();
        this.deskpane.add(clave);
        clave.setTitle("Cambiar Contraseña de Usuario");
        clave.setVisible(true);

    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        // TODO add your handling code here:
        GestorUsuarios gu=new GestorUsuarios();
        this.deskpane.add(gu);
        gu.setTitle("Gestión de Usuarios del sistema");
        gu.setVisible(true);
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        new Login().setVisible(true);
        dispose();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        Acerca acerca=new Acerca();
        acerca.setTitle("Acerca de NeoMarket");
        acerca.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void txtComandKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComandKeyPressed
        // TODO add your handling code here:
        this.iniciado=true;
        if(evt.getKeyCode() == KeyEvent.VK_F1) {
            // Enter was pressed. Your code goes here.
            this.btnVentaActionPerformed(null);
        }
    }//GEN-LAST:event_txtComandKeyPressed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        // TODO add your handling code here:
        
    }//GEN-LAST:event_formFocusGained

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        // TODO add your handling code here:
        if(this.iniciado==true){
            this.lblestado.setText("Seleccione una funcion del menú a través del teclado o el mouse.");
        } else {
            this.lblestado.setText("Bienvenido (a) al sistema de ventas NeoMarket. Seleccione la funcion que desee realizar.");
        }
        
    }//GEN-LAST:event_formWindowGainedFocus

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowActivated

    private void deskpaneComponentRemoved(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_deskpaneComponentRemoved
        // TODO add your handling code here:
        //Funcion de retoma de comandos por teclado desde JInternalFrame
        this.inputCommand();
    }//GEN-LAST:event_deskpaneComponentRemoved

    private void deskpaneComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_deskpaneComponentHidden
        // TODO add your handling code here:
    }//GEN-LAST:event_deskpaneComponentHidden

    private void deskpaneComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_deskpaneComponentMoved
        // TODO add your handling code here:
        
    }//GEN-LAST:event_deskpaneComponentMoved

    private void deskpaneComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_deskpaneComponentResized
        // TODO add your handling code here:
        
    }//GEN-LAST:event_deskpaneComponentResized

    private void deskpaneComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_deskpaneComponentShown
        // TODO add your handling code here:
        
    }//GEN-LAST:event_deskpaneComponentShown

    private void deskpaneFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_deskpaneFocusGained
        // TODO add your handling code here:
        
    }//GEN-LAST:event_deskpaneFocusGained

    private void deskpanePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_deskpanePropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_deskpanePropertyChange

    private void deskpaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deskpaneMouseClicked
        // TODO add your handling code here:
        this.inputCommand();
    }//GEN-LAST:event_deskpaneMouseClicked

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        proveedores.setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Escritorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Escritorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Escritorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Escritorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new Escritorio().setVisible(true);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JMenuBar Menu;
    private javax.swing.JMenuItem btnVenta;
    private javax.swing.JDesktopPane deskpane;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    public javax.swing.JLabel lblestado;
    private javax.swing.JMenu mnSistema;
    private javax.swing.JMenu mnUser;
    public javax.swing.JTextField txtComand;
    // End of variables declaration//GEN-END:variables
}
